// pages/index/order_detail/order_detail.js

//支付方式
//1：微信支付 2：美团支付 3：余额支付

const http = require('../../utils/http')
const api = require('../../utils/api')
const ui = require('../../utils/ui')

import Dialog from "../../components/dialog/dialog"


Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRenewal: false,
    oid: "",
    name: "",
    desc: "",
    url: "",
    time: "",
    duration: "",
    price: "",
    memberPrice: "",
    paymentSeleted: 0,
    payment: [
      {
        name: "余额支付",
        icon: "/pages/images/icon_mt.png",
        mode: 3
      },
      {
        name: "抖音验券",
        icon: "/pages/images/icon_dy.png",
        mode: 4,
      },
      {
        name: "微信支付",
        icon: "/pages/images/icon_wechat.png",
        mode: 1
      },
    ],
    code: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      isRenewal: JSON.parse(options.isRenewal),
      oid: options.oid,
      name: options.name,
      desc: options.desc,
      url: decodeURIComponent(options.url),
      time: options.time,
      duration: options.duration,
      price: options.price, 
      memberPrice: options.memberPrice ?? options.price,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  paste() {
    wx.getClipboardData({
      success: (option) => {
        this.setData({
          code: option.data,
        })
      },
    })
  },

  showDouyinDialog() {
    let mode = this.data.payment[this.data.paymentSeleted].mode
    Dialog.confirm({
      selector: "#dy-dialog",
      title: "抖音团购券码",
      cancelButtonText: "取消",
      confirmButtonText: "验券",
      confirmCallback:() => {
        if (this.data.code == "") {
          ui.showToast({
            title: "请输入券码",
          })
        }
        else {
          http.request({
            url: api.pay,
            method: "POST",
            data: {
              payMode: mode,
              couponCode: this.data.code,
              tradeId: this.data.oid,
              memberId: getApp().globalData.memberId
            }
          }).then(res=>{
            let data = res.data.result
            if (data.state) {
              ui.showToast({
                title: "支付成功",
              })
              wx.switchTab({
                url: '/pages/order/order',
              })
            }
            else {
              ui.showToast({
                title: data.msg,
              })
            }
          })
        }
      },
    });
  },
  
  radioChange: function(e) {
    this.setData({
      paymentSeleted: e.detail.value
    })
  },

  switchMode(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      paymentSeleted: index
    })
  },

  pay() {
    let mode = this.data.payment[this.data.paymentSeleted].mode
    if (mode == 4) {
      this.showDouyinDialog()
    }
    else {
      http.request({
        url: api.pay,
        method: "POST",
        data: {
          payMode: mode,
          tradeId: this.data.oid,
          memberId: getApp().globalData.memberId
        }
      }).then(res=>{
        let data = res.data.result
        if (data.state) {
          if (mode == 1) {
            let prepayObj = res.data.result.prepayVO
            // prepayObj.package = "prepay_id="+prepayObj.prepayId
            prepayObj.package = prepayObj.packages
            prepayObj.signType = "RSA"
            prepayObj.timeStamp += ""
            try {
              // 得到接口返回的数据，向微信发起支付
              wx.requestPayment({
                ...prepayObj,
                success:(res)=>{
                  ui.showToast({
                    title: "支付成功",
                  })
                  wx.switchTab({
                    url: '/pages/order/order',
                  })
                },
                fail:(res)=>{
                  console.log("支付失败", res)
                },
                complete:(res)=>{
                  console.log("支付完成", res)
                }
              });
            } catch (err) {
              ui.showToast({
                title: "支付失败",
              });
            }
          }
          else if (mode == 3) {
            //余额支付成功
            ui.showToast({
              title: "支付成功",
            })
            wx.switchTab({
              url: '/pages/order/order',
            })
          }
        }
        else {
          ui.showToast({
            // icon: "error",
            title: data.msg,
          })
        }
      })
    }
    
  }

})